var util = require('../../../../utils/util.js');
var getLessLimitSizeImage = require('../../../../utils/zipPic.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //画板边长默认是屏幕宽度，正方形画布
    cw:wx.getSystemInfoSync().windowWidth,
    star_content:{'1':'非常差','2':'差','3':'一般','4':'满意','5':'非常满意'},
    star_list:[0,1,2,3,4],
    fuwu_num:-1,
    zhiliang:-1,
    kouwei:-1,
    iseval:false,  //提交评价
    upimg_list:[],
    PJ_content:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    });
    this.comment_list();
  },
  // 评价详情
  comment_list:function(){
    var that = this;
    wx.request({  
      url: app.globalData.url + 'get_order_comment/'+that.data.id,
      method: "GET",
      success: function(res) {
        console.log(JSON.stringify(res.data));  
        that.setData({
          comm_list:res.data
        });
      }
    });  
  },
  inputPJ:function(e){
    this.setData({
      PJ_content:e.detail.value
    });
  },
  // 提交评价
  submit_ck:function(){
    if(this.data.comm_list.length==0 && (this.data.fuwu_num==-1 || this.data.zhiliang==-1 || this.data.kouwei==-1)){
      wx.showModal({
        title: '提示',
        content: "请为服务、质量、价格打星",
        showCancel: false,
        confirmText:"我知道了",
        success: function (res) {}
      }); 
      return;
    };
    if((this.data.comm_list.length>0 || this.data.upimg_list.length>0) && this.data.PJ_content==''){
      wx.showLoading({
        title:'请输入评价内容',
        duration:500
      });
      return;
    }
    this.setData({
      iseval:true
    });
  },
  cancel_eval:function(){
    this.setData({
      iseval:false
    });
  },
  conf_eval:function(){
    var upconf={};
    upconf.time=util.formatTime(new Date());
    upconf.img=this.data.upimg_list;
    upconf.content=this.data.PJ_content;
    var cover_str=upconf['img'].join(',');
    var that = this;
    console.log(that.data.fuwu_num+1);
    console.log(that.data.zhiliang+1);
    console.log(that.data.kouwei+1);
    that.setData({
      iseval:false,
      PJ_content:'',
      upimg_list:[]
    });
    console.log(cover_str);
    app.AppHttp('save_order_comment', {
      openid:app.globalData.openid,
      order_id:that.data.id,
      star1:that.data.fuwu_num+1,
      star2:that.data.zhiliang+1,
      star3:that.data.kouwei+1,
      content:upconf.content,
      cover_str:cover_str
    }, function(res) {
      console.log(JSON.stringify(res.data));
      that.setData({
        fuwu_num:-1,
        zhiliang:-1,
        kouwei:-1
      });
      that.comment_list();
    })
  },
  fuwuTap:function(e){
    var index=e.currentTarget.dataset.index;
    if(index<=this.data.fuwu_num){
      this.setData({
        fuwu_num:index-1
      });
    }else{
      this.setData({
        fuwu_num:index
      });
    }; 
  },
  zhiTap:function(e){
    var index=e.currentTarget.dataset.index;
    if(index<=this.data.zhiliang){
      this.setData({
        zhiliang:index-1
      });
    }else{
      this.setData({
        zhiliang:index
      });
    };
  },
  kouTap:function(e){
    var index=e.currentTarget.dataset.index;
    if(index<=this.data.kouwei){
      this.setData({
        kouwei:index-1
      });
    }else{
      this.setData({
        kouwei:index
      });
    };
  },
  add_up_img:function(){
    var that = this;
    var count=3-parseInt(that.data.upimg_list.length); 
    wx.chooseImage({
      count:count,
      sizeType: ['original'],//原图
      sourceType: [ 'album','camera'],//支持选取图片
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        that.upload_img(tempFilePaths,0,tempFilePaths.length);
      }
    })
  }, 
  // 上传图片
  upload_img:function(tempFilePaths,change_len,len){
    var that = this;
    len=parseInt(len);
    change_len=parseInt(change_len);
    let canvasId='zipCanvas' //注意这里的id和你在页面中写的html代码的canvas的id要一致
    let imagePath=tempFilePaths[change_len];//原图的路径
    let limitSize=300;//大小限制300kb
    let drawWidth=wx.getSystemInfoSync().windowWidth;//初始绘画区域是画布自身的宽度也就是屏幕宽度
    wx.showLoading({title:'图片压缩中...',mask:true}) //不需要你可以删掉
    getLessLimitSizeImage.getLessLimitSizeImage(canvasId,imagePath,limitSize,drawWidth,(resPath)=>{
      wx.hideLoading(); //不需要你可以删掉  
      //resPath就是压缩后图片的路径，然后想做什么都随你
      console.log(resPath);
      wx.uploadFile({
        url: app.globalData.url + 'api/Comment/uploadCommentCover',
        // filePath:tempFilePaths[change_len],
        filePath:resPath,
        name:'com_cover',
        method: "POST",
        formData: {
        },
        success: function (res) {
          var data=JSON.parse(res.data);
          var upimg_list=that.data.upimg_list;
          upimg_list.push(data.msg);
          that.setData({
            upimg_list:upimg_list
          });
          console.log(change_len);
          if(change_len<len-1){
            change_len++;
            that.upload_img(tempFilePaths,change_len,len);
          };  
          console.log(upimg_list);
        },
        fail: function (res) {
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false
          });
        }
      });
    })
  },
  del_img_but:function(e){
    var index=e.currentTarget.dataset.index;
    var upimg_list=this.data.upimg_list;
    upimg_list.splice(index,1);
    this.setData({
      upimg_list:upimg_list
    });
  },
  /**
   * 图片预览
   */
  previewImg: function (e) {
    var eal = e.currentTarget.dataset.eal;
    var arr=e.currentTarget.dataset.item;
    var imgArr;
    imgArr = arr.map((item,index) =>{
      return 'http://www.xiaobangcai.com'+item.com_cover
    });
    wx.previewImage({
      current: imgArr[eal], //当前图片地址
      urls: imgArr, //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})